<?php

// $Id $

class DpUserProfile
{
    private $_elements;
    private $_username;

    function __construct($username) {
        global $dpdb;
//        global $User;

        $this->_username = $username;

        if(! $dpdb->SqlExists("
            SELECT 1 FROM users AS u
            JOIN user_profiles AS up 
                ON u.username = up.username
                AND u.profilename = up.profilename
            WHERE u.username = '$username'"))
        {
            $dpdb->SqlExecute("
                UPDATE users
                SET profilename = 'default'
                WHERE username = '$username'");

            if(! $dpdb->SqlExists("
                SELECT 1 FROM user_profiles
                WHERE username = '$username'
                    AND profilename = 'default'"))
            {
                $dpdb->SqlExecute("
                    INSERT INTO user_profiles
                        (username, profilename)
                    VALUES
                        ('$username', 'default')");
            }
        }

        $this->_elements = $dpdb->SqlOneRow("
            SELECT
                    u.profilename,
                    u_lang,
                    u_privacy,
                    i_theme,
                    i_res,
                    u_intlang,
                    email_updates,
                    u_neigh,
                    i_pmdefault,
                    i_layout,
                    v_zoom,
                    h_zoom,
                    v_tframe,
                    h_tframe,
                    v_tlines,
                    h_tlines,
                    v_tchars,
                    h_tchars,
                    v_twrap,
                    h_twrap,
                    v_tscroll,
                    h_tscroll,
                    v_font_face,
                    h_font_face,
                    v_font_size,
                    h_font_size,
                    i_toolbar,
                    i_statusbar,
                    i_newwin
            FROM users AS u
            JOIN user_profiles AS up
                ON u.username = up.username
                AND u.profilename = up.profilename
            WHERE u.username = '$username'");
    }

    public function IsNeighbors() {
        return $this->_elements['u_neigh'];
    }

    public function Neighbors() {
        return $this->_elements['u_neigh'];
    }

    public function IsEmailUpdates() {
        return $this->_elements['email_updates'];
    }

    public function LastActivityTimeInt() {
        return $this->_elements['t_last_activity'];
    }

    public function ProfileName() {
        return $this->_elements['profilename'];
    }

    public function LayoutIndex() {
        return $this->_elements['i_layout'];
    }

/*
    public function InterfaceLanguage()
    {
        return $this->_elements['u_intlang'];
    }
*/

/*
    public function InterfaceIndex()
    {
        return $this->_elements['i_type'];
    }
*/
    public function IsNewWindow() {
        return $this->_elements['i_newwin'];
    }

    public function ScreenResolution() {
        return $this->_elements['i_res'];
    }
    public function VerticalFontFace() {
        return preg_replace(
                "/\_/", " ", $this->_elements['v_font_face']);
    }

    public function HorizontalFontFace() {
        return preg_replace(
                "/\_/", " ", $this->_elements['h_font_face']);
    }

    public function FontFace() {
        return ($this->IsVerticalLayout()
            ? $this->VerticalFontFace()
            : $this->HorizontalFontFace());
    }

    public function VerticalFontSize() {
        return $this->_elements['v_font_size'];
    }

    public function HorizontalFontSize() {
        return $this->_elements['h_font_size'];
    }

    public function FontSize() {
        return ($this->IsVerticalLayout()
            ? $this->VerticalFontSize()
            : $this->HorizontalFontSize());
    }

    public function Istoolbar() {
        return $this->_elements['i_toolbar'];
    }

    public function ShowStatusBar() {
        return $this->_elements['i_statusbar'];
    }

    public function IsVerticalLayout() {
        return ($this->_elements['i_layout'] != 0);
    }

    public function VerticalTextChars() {
            return $this->_elements['v_tchars'];
    }
    public function HorizontalTextChars() {
            return $this->_elements['h_tchars'];
    }
    public function TextChars() {
        return ($this->IsVerticalLayout()
            ? $this->VerticalTextChars()
            : $this->HorizontalTextChars());
    }

    public function VerticalZoom() {
            return $this->_elements['v_zoom'];
    }

    public function HorizontalZoom() {
            return $this->_elements['h_zoom'];
    }

    public function Zoom() {
        return ($this->IsVerticalLayout()
            ? $this->VerticalZoom()
            : $this->HorizontalZoom());
    }

//    private function SetZoom($zoom) {
////        global $dpdb;
//        if($zoom < 25)
//            $zoom = 25;
//        if($zoom > 400)
//            $zoom = 400;
//        if($this->IsVerticalLayout())
//            $this->set_active_profile_field("v_zoom", $zoom);
//        else
//            $this->set_active_profile_field("h_zoom", $zoom);
//    }
//
    public function VerticalTextLines() {
            return $this->_elements['v_tlines'];
    }
    public function HorizontalTextLines() {
            return $this->_elements['h_tlines'];
    }
    public function TextLines() {
        return ($this->IsVerticalLayout()
                    ? $this->VerticalTextLines()
                    : $this->HorizontalTextLines());
    }

    public function TextColumns() {
        return $this->TextChars();
    }

    public function VerticalTextFramePct() {
        return $this->normalize_pct($this->_elements['v_tframe']);
    }

    private function normalize_pct($pct) {
        if($pct < 20) $pct = 20;
        if($pct > 80) $pct = 80;
        return $pct;
    }

    public function HorizontalTextFramePct() {
        return $this->normalize_pct($this->_elements['h_tframe']);
    }

    public function TextFramePct() {
        return ($this->IsVerticalLayout()
                    ? $this->VerticalTextFramePct()
                    : $this->HorizontalTextFramePct());
    }

//    public function TextCols() {
//        return ($this->IsVerticalLayout()
//                    ? $this->VerticalTextCols()
//                    : $this->HorizontalTextCols());
//    }

    public function VerticalWrap() {
            return $this->_elements['v_twrap'];
    }

    public function HorizontalWrap() {
            return $this->_elements['h_twrap'];
    }


    public function TextWrap() {
        return ($this->IsVerticalLayout()
                    ? $this->VerticalWrap()
                    : $this->HorizontalWrap());
    }

    public function IsVerticalTextScroll() {
            return $this->_elements['v_tscroll'];
    }

    public function IsHorizontalTextScroll() {
            return $this->_elements['h_tscroll'];
    }

//    public function IsTextScroll() {
//        return ($this->IsVerticalLayout()
//                    ? $this->IsVerticalTextScroll()
//                    : $this->IsHorizontalTextScroll());
//    }

//    public function Privacy() {
//        return $this->_elements['u_privacy'];
//    }

//    public function IsPrivate() {
//        return $this->_elements['u_privacy'] == PRIVACY_PRIVATE;
//    }
//
//    public function IsAnonymous() {
//        return $this->_elements['u_privacy'] == PRIVACY_ANONYMOUS;
//    }
//
//    public function IsPublic() {
//        return $this->_elements['u_privacy'] == PRIVACY_PUBLIC;
//    }
}
?>
